Skip to content

feat: block time average#5504

Merged
sbackend123 merged 7 commits into
masterfrom
fix/add-average-block-time
Jun 18, 2026
Merged

feat: block time average#5504
sbackend123 merged 7 commits into
masterfrom
fix/add-average-block-time

Conversation

@sbackend123

@sbackend123 sbackend123 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Checklist

  • I have read the coding guide.
  • My change requires a documentation update, and I have done it.
  • I have added tests to cover my changes.
  • I have filled out the description and linked the related issues.

Description

Added average block time for cache RPC call BlockNumber(). Average block time is computed as (newHeader.Time − prevAnchor.timestamp) / (newHeader.Number − prevAnchor.number) on each cache refresh, using consensus timestamps from the polled headers. If there is no previous anchor (prev.number == 0), or the block number or timestamp did not strictly increase, the configured default block time is used instead.

Observed intervals above maxAverageBlockTime (30 seconds) are capped, so a one-off long gap (chain stall) cannot inflate extrapolation for an extended period. It prevents case when chain got stuck and then cache used "stuck" block average for next N blocks (what can be hours).

Open API Spec Version Changes (if applicable)

Motivation and Context (Optional)

On-chain block time is not constant. When the node extrapolates block numbers using a fixed configured block time, the estimate drifts further from the real chain between RPC refreshes. This drift can be significant in redistribution game scenarios, where phase timing depends on accurate block numbers.

Related Issue (Optional)

#5485

Screenshots (if appropriate):

AI Disclosure

  • This PR contains code that has been generated by an LLM.
  • I have reviewed the AI generated code thoroughly.
  • I possess the technical expertise to responsibly review the code generated in this PR.

@sbackend123 sbackend123 marked this pull request as ready for review June 17, 2026 14:45
Comment thread pkg/transaction/wrapped/wrapped.go
Comment thread pkg/transaction/wrapped/wrapped.go Outdated
if prev.number == 0 || newNumber <= prev.number {
return b.blockTime
}
if !newTime.After(prev.timestamp) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not have this check also on the first if?

@gacevicljubisa gacevicljubisa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nice refinement and improvement on the anchor/extrapolation approach we discussed in #5394

Comment thread pkg/transaction/wrapped/wrapped.go Outdated
}

// computeAverageBlockTime returns the observed block time between prev and the
// freshly loaded header. Falls back to the configured block time only when prev

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think comment is outdated because it also falls back when newNumber <= prev.number and when !newTime.After(prev.timestamp)

blocks := newNumber - prev.number

averageBlockTime := elapsed / time.Duration(blocks)
if averageBlockTime > maxAverageBlockTime {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have a lower bound as minAverageBlockTime, for maybe some misbehaving RPCs returning a large block jump with a near-equal timestamp ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it is actually possible...

@sbackend123 sbackend123 merged commit fe87393 into master Jun 18, 2026
20 of 21 checks passed
@sbackend123 sbackend123 deleted the fix/add-average-block-time branch June 18, 2026 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants